package net.tv90.bfmccore.task;

import lk.vexview.api.VexViewAPI;
import lk.vexview.gui.components.VexImage;
import lk.vexview.hud.VexImageShow;
import net.tv90.bfmccore.game.GameManager;
import net.tv90.bfmccore.util.area.*;
import net.tv90.bfmccore.util.Cooldown;
import net.tv90.bfmccore.util.vector.Vector2i;
import net.tv90.bfmccore.util.vector.Vector3i;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static net.tv90.bfmccore.helper.FactionHelper.getCountry2;
import static net.tv90.bfmccore.util.area.CombatArea.isOutOfCombatArea;

public class CombatAreaCheckTask implements Runnable{

    //储存战斗区域
    public static List<CombatArea> combatAreaList = new ArrayList<>();

    static {
        RectangleArea2i area2i = new RectangleArea2i(new Vector2i(-331,-437), new Vector2i(364,359));
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(area2i);
        CombatArea COMBATAREA_RU = new CombatArea("world", "RU", 8, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("world", "US", 8, area2iList);
        CombatArea COMBATAREA_CN = new CombatArea("world", "CN", 8, area2iList);
        CombatArea COMBATAREA = new CombatArea("world", "NoFaction", 8, area2iList);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
        combatAreaList.add(COMBATAREA_RU);
        combatAreaList.add(COMBATAREA);
    }

    //用于测试
    static {
        List<Vector2i> listRU1 = new ArrayList<>();
        listRU1.add(new Vector2i(-380,-859));
        listRU1.add(new Vector2i(-329,-859));
        listRU1.add(new Vector2i(-329,-780));
        listRU1.add(new Vector2i(-380,-780));
        Area2i AREA_JUNGLETEMPLE_RU_1 = new Area2i(listRU1);
        List<Vector2i> listUS1 = new ArrayList<>();
        listUS1.add(new Vector2i(-325,-877));
        listUS1.add(new Vector2i(-380,-877));
        listUS1.add(new Vector2i(-380,-797));
        listUS1.add(new Vector2i(-364,-789));
        listUS1.add(new Vector2i(-338,-790));
        listUS1.add(new Vector2i(-325,-801));
        Area2i AREA_JUNGLETEMPLE_US_1 = new Area2i(listUS1);
        List<Vector2i> listRU2 = new ArrayList<>();
        listRU2.add(new Vector2i(-336,-748));
        listRU2.add(new Vector2i(-336,-734));
        listRU2.add(new Vector2i(-363,-734));
        listRU2.add(new Vector2i(-363,-748));
        Area2i AREA_JUNGLETEMPLE_RU_2 = new Area2i(listRU2);
        List<Vector2i> listUS2 = new ArrayList<>();
        listUS2.add(new Vector2i(-336,-748));
        listUS2.add(new Vector2i(-336,-734));
        listUS2.add(new Vector2i(-363,-734));
        listUS2.add(new Vector2i(-363,-748));
        Area2i AREA_JUNGLETEMPLE_US_2 = new Area2i(listUS2);
        List<Area> area2iList_RU = new ArrayList<>();
        List<Area> area2iList_US = new ArrayList<>();
        area2iList_RU.add(AREA_JUNGLETEMPLE_RU_1);
        area2iList_RU.add(AREA_JUNGLETEMPLE_RU_2);
        area2iList_US.add(AREA_JUNGLETEMPLE_US_1);
        area2iList_US.add(AREA_JUNGLETEMPLE_US_2);
        CombatArea COMBATAREA_JUNGLETEMPLE_RU = new CombatArea("JungleTemple", "RU", 5, area2iList_RU);
        CombatArea COMBATAREA_JUNGLETEMPLE_US = new CombatArea("JungleTemple", "US", 5, area2iList_US);
        combatAreaList.add(COMBATAREA_JUNGLETEMPLE_RU);
        combatAreaList.add(COMBATAREA_JUNGLETEMPLE_US);
    }

    static {
        List<Vector2i> list = new ArrayList<>();
        list.add(new Vector2i(310, 668));
        list.add(new Vector2i(310, 579));
        list.add(new Vector2i(153, 579));
        list.add(new Vector2i(153, 668));
        Area2i area2i = new Area2i(list);
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(area2i);
        CombatArea COMBATAREA_CN = new CombatArea("SiegeOfShanghaiTDM", "CN", 7, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("SiegeOfShanghaiTDM", "US", 7, area2iList);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(new CircleArea(new Vector2i(52,-38), 150, 50, 128));
        CombatArea COMBATAREA_CN = new CombatArea("HainanResortTDM", "CN", 6, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("HainanResortTDM", "US", 6, area2iList);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(new CircleArea(new Vector2i(-39,-223), 70, 0, 128));
        CombatArea COMBATAREA_CN = new CombatArea("ParacelStormTDM", "CN", 8, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("ParacelStormTDM", "US", 8, area2iList);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(new RectangleArea3i(new Vector3i(-117, 0 , -512), new Vector3i(40, 130, -185)));
        List<Area> area2iList2 = new ArrayList<>();
        area2iList2.add(new RectangleArea3i(new Vector3i(-117, 0 , -411), new Vector3i(40, 130, -128)));
        CombatArea COMBATAREA_CN = new CombatArea("ParacelStormCQS", "CN", 8, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("ParacelStormCQS", "US", 8, area2iList2);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        RectangleArea3i area2i = new RectangleArea3i(new Vector3i(267,12,803), new Vector3i(159,512,720));
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(area2i);
        CombatArea COMBATAREA_CN = new CombatArea("SiegeOfShanghaiTDM2", "CN", 5, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("SiegeOfShanghaiTDM2", "US", 5, area2iList);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        RectangleArea3i area2i = new RectangleArea3i(new Vector3i(715,82,-665), new Vector3i(-649,400,741));
        RectangleArea3i spawn = new RectangleArea3i(new Vector3i(-10,56,137), new Vector3i(2,62,133));
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(area2i);
        area2iList.add(spawn);
        CombatArea COMBATAREA_CN = new CombatArea("WakeIslandAirSuperiority", "CN", 20, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("WakeIslandAirSuperiority", "US", 20, area2iList);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        RectangleArea2i area2i = new RectangleArea2i(new Vector2i(-2524,1554), new Vector2i(-2594,1677));
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(area2i);
        CombatArea COMBATAREA_CN = new CombatArea("PropagandaTDM", "CN", 7, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("PropagandaTDM", "US", 7, area2iList);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        RectangleArea2i area2i_cn = new RectangleArea2i(new Vector2i(51,-2530), new Vector2i(-150,-2470));
        RectangleArea2i area2i_ru = new RectangleArea2i(new Vector2i(100,-2530), new Vector2i(-96,-2470));
        List<Area> area2iList_cn = new ArrayList<>();
        area2iList_cn.add(area2i_cn);
        List<Area> area2iList_ru = new ArrayList<>();
        area2iList_ru.add(area2i_ru);
        CombatArea COMBATAREA_CN = new CombatArea("CruiseShip", "CN", 6, area2iList_cn);
        CombatArea COMBATAREA_US = new CombatArea("CruiseShip", "RU", 6, area2iList_ru);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        RectangleArea2i area2i = new RectangleArea2i(new Vector2i(957,-214), new Vector2i(1101,-88));
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(area2i);
        CombatArea COMBATAREA_CN = new CombatArea("KhargIslandTDM", "RU", 5, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("KhargIslandTDM", "US", 5, area2iList);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        RectangleArea2i area2i = new RectangleArea2i(new Vector2i(510, 146), new Vector2i(392, 251));
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(area2i);
        CombatArea COMBATAREA_CN = new CombatArea("NoushahrCanals", "RU", 7, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("NoushahrCanals", "US", 7, area2iList);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        RectangleArea3i area = new RectangleArea3i(new Vector3i(116, 0 ,-84) , new Vector3i(-100,105,237));
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(area);
        CombatArea COMBATAREA_CN = new CombatArea("AlpineTrain", "RU", 8, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("AlpineTrain", "CN", 8, area2iList);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        RectangleArea2i area_us = new RectangleArea2i(new Vector2i(1188,706), new Vector2i(674,-321));
        RectangleArea2i area_ru = new RectangleArea2i(new Vector2i(1177,121), new Vector2i(671,-510));
        RectangleArea2i spawn = new RectangleArea2i(new Vector2i(515,-159), new Vector2i(529,-190));
        List<Area> area2iList_us = new ArrayList<>();
        List<Area> area2iList_ru = new ArrayList<>();
        area2iList_us.add(area_us);
        area2iList_ru.add(area_ru);
        area2iList_us.add(spawn);
        area2iList_ru.add(spawn);
        CombatArea COMBATAREA_US = new CombatArea("KhargIsland", "US", 20, area2iList_us);
        CombatArea COMBATAREA_RU = new CombatArea("KhargIsland", "RU", 20, area2iList_ru);
        combatAreaList.add(COMBATAREA_US);
        combatAreaList.add(COMBATAREA_RU);
    }

    static {
        RectangleArea2i area_us = new RectangleArea2i(new Vector2i(1188,706), new Vector2i(674,-321));
        RectangleArea2i area_ru = new RectangleArea2i(new Vector2i(1177,121), new Vector2i(671,-510));
        RectangleArea2i spawn = new RectangleArea2i(new Vector2i(515,-159), new Vector2i(529,-190));
        List<Area> area2iList_us = new ArrayList<>();
        List<Area> area2iList_ru = new ArrayList<>();
        area2iList_us.add(area_us);
        area2iList_ru.add(area_ru);
        area2iList_us.add(spawn);
        area2iList_ru.add(spawn);
        CombatArea COMBATAREA_US = new CombatArea("KhargIslandVS", "US", 20, area2iList_us);
        CombatArea COMBATAREA_RU = new CombatArea("KhargIslandVS", "RU", 20, area2iList_ru);
        combatAreaList.add(COMBATAREA_US);
        combatAreaList.add(COMBATAREA_RU);
    }

    static {
        RectangleArea2i area_us = new RectangleArea2i(new Vector2i(-387,-692), new Vector2i(-326,-876));
        RectangleArea2i area_ru = new RectangleArea2i(new Vector2i(-387,-664), new Vector2i(-326,-854));
        RectangleArea2i spawn = new RectangleArea2i(new Vector2i(-415,-785), new Vector2i(-419,-773));
        List<Area> area2iList_us = new ArrayList<>();
        List<Area> area2iList_ru = new ArrayList<>();
        area2iList_us.add(area_us);
        area2iList_ru.add(area_ru);
        area2iList_us.add(spawn);
        area2iList_ru.add(spawn);
        CombatArea COMBATAREA_US = new CombatArea("JungleTempleCQ", "US", 5, area2iList_us);
        CombatArea COMBATAREA_RU = new CombatArea("JungleTempleCQ", "CN", 5, area2iList_ru);
        combatAreaList.add(COMBATAREA_US);
        combatAreaList.add(COMBATAREA_RU);
    }

    static {
        List<Vector2i> list = new ArrayList<>();
        list.add(new Vector2i(47, -112));
        list.add(new Vector2i(47, 15));
        list.add(new Vector2i(-80, 15));
        list.add(new Vector2i(-80, -112));
        Area2i area2i = new Area2i(list);
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(area2i);
        CombatArea COMBATAREA_RU = new CombatArea("Zavod311", "RU", 5, area2iList);
        CombatArea COMBATAREA_CN = new CombatArea("Zavod311", "CN", 5, area2iList);
        combatAreaList.add(COMBATAREA_RU);
        combatAreaList.add(COMBATAREA_CN);
    }

    static {
        List<Vector2i> list = new ArrayList<>();
        list.add(new Vector2i(1252, 2020));
        list.add(new Vector2i(1252, 2093));
        list.add(new Vector2i(1155, 2093));
        list.add(new Vector2i(1155, 2020));
        Area2i area2i = new Area2i(list);
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(area2i);
        CombatArea COMBATAREA_RU = new CombatArea("Scrapmetal", "RU", 6, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("Scrapmetal", "US", 6, area2iList);
        combatAreaList.add(COMBATAREA_RU);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        List<Vector2i> list = new ArrayList<>();
        list.add(new Vector2i(1251, -34));
        list.add(new Vector2i(1251, 107));
        list.add(new Vector2i(1093, 107));
        list.add(new Vector2i(1093, -34));
        Area2i area2i = new Area2i(list);
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(area2i);
        CombatArea COMBATAREA_RU = new CombatArea("CaspianBorderTDM", "RU", 5, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("CaspianBorderTDM", "US", 5, area2iList);
        combatAreaList.add(COMBATAREA_RU);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        RectangleArea3i area = new RectangleArea3i(new Vector3i(421, 10 ,986) , new Vector3i(115,256,490));
        Area area_A = new RectangleArea3i(new Vector3i(265, 8, 945), new Vector3i(156, 18, 893));
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(area);
        area2iList.add(area_A);
        CombatArea COMBATAREA_US = new CombatArea("SiegeOfShanghaiCQS", "US", 8, area2iList);
        CombatArea COMBATAREA_CN = new CombatArea("SiegeOfShanghaiCQS", "CN", 8, area2iList);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        RectangleArea3i area2i = new RectangleArea3i(new Vector3i(267,12,803), new Vector3i(159,512,720));
        Area area_A = new RectangleArea3i(new Vector3i(272, 8, 837), new Vector3i(142, 150, 958));
        List<Area> area2iList = new ArrayList<>();
        area2iList.add(area2i);
        area2iList.add(area_A);
        CombatArea COMBATAREA_US = new CombatArea("SiegeOfShanghaiSDM", "US", 8, area2iList);
        CombatArea COMBATAREA_CN = new CombatArea("SiegeOfShanghaiSDM", "CN", 8, area2iList);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        List<Vector2i> list = new ArrayList<>();
        list.add(new Vector2i(-2020,1273));
        list.add(new Vector2i(-1637,1273));
        list.add(new Vector2i(-1637, 1465));
        list.add(new Vector2i(-2020,1465));
        Area2i area2i = new Area2i(list);
        List<Vector2i> list2 = new ArrayList<>();
        list2.add(new Vector2i(-2134,1273));
        list2.add(new Vector2i(-1711,1273));
        list2.add(new Vector2i(-1711, 1465));
        list2.add(new Vector2i(-2134, 1465));
        Area2i area2i2 = new Area2i(list2);
        List<Area> area2iList = new ArrayList<>();
        List<Area> area2iList2 = new ArrayList<>();
        area2iList.add(area2i);
        area2iList2.add(area2i2);
        CombatArea COMBATAREA_US = new CombatArea("OperationMetro", "US", 8, area2iList);
        CombatArea COMBATAREA_RU = new CombatArea("OperationMetro", "RU", 8, area2iList2);
        combatAreaList.add(COMBATAREA_US);
        combatAreaList.add(COMBATAREA_RU);
    }

    static {
        RectangleArea2i area_us = new RectangleArea2i(new Vector2i(-1912,1420), new Vector2i(-1737,1320));
        RectangleArea2i area_ru = new RectangleArea2i(new Vector2i(-1945,1420), new Vector2i(-1767,1320));
        RectangleArea2i spawn = new RectangleArea2i(new Vector2i(-2008,1427), new Vector2i(-1980,1415));
        List<Area> area2iList_us = new ArrayList<>();
        List<Area> area2iList_ru = new ArrayList<>();
        area2iList_us.add(area_us);
        area2iList_ru.add(area_ru);
        area2iList_us.add(spawn);
        area2iList_ru.add(spawn);
        CombatArea COMBATAREA_US = new CombatArea("OperationMetroCQS", "US", 5, area2iList_us);
        CombatArea COMBATAREA_RU = new CombatArea("OperationMetroCQS", "RU", 5, area2iList_ru);
        combatAreaList.add(COMBATAREA_US);
        combatAreaList.add(COMBATAREA_RU);
    }

    static {
        List<Vector2i> list = new ArrayList<>();
        list.add(new Vector2i(10,-119));
        list.add(new Vector2i(-128,-119));
        list.add(new Vector2i(-128, -553));
        list.add(new Vector2i(10, -553));
        Area2i area2i = new Area2i(list);
        List<Vector2i> list2 = new ArrayList<>();
        list2.add(new Vector2i(10,-194));
        list2.add(new Vector2i(-128,-194));
        list2.add(new Vector2i(-128, -615));
        list2.add(new Vector2i(10, -615));
        Area2i area2i2 = new Area2i(list2);
        List<Area> area2iList = new ArrayList<>();
        List<Area> area2iList2 = new ArrayList<>();
        area2iList.add(area2i);
        area2iList2.add(area2i2);
        CombatArea COMBATAREA_CN = new CombatArea("OperationLocker", "CN", 8, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("OperationLocker", "US", 8, area2iList2);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static {
        List<Vector2i> list = new ArrayList<>();
        list.add(new Vector2i(10,-119));
        list.add(new Vector2i(-128,-119));
        list.add(new Vector2i(-128, -468));
        list.add(new Vector2i(10, -468));
        Area2i area2i = new Area2i(list);
        List<Vector2i> list2 = new ArrayList<>();
        list2.add(new Vector2i(10,-244));
        list2.add(new Vector2i(-128,-244));
        list2.add(new Vector2i(-128, -615));
        list2.add(new Vector2i(10, -615));
        Area2i area2i2 = new Area2i(list2);
        List<Area> area2iList = new ArrayList<>();
        List<Area> area2iList2 = new ArrayList<>();
        area2iList.add(area2i);
        area2iList2.add(area2i2);
        CombatArea COMBATAREA_CN = new CombatArea("OperationLockerCQS", "CN", 5, area2iList);
        CombatArea COMBATAREA_US = new CombatArea("OperationLockerCQS", "US", 5, area2iList2);
        combatAreaList.add(COMBATAREA_CN);
        combatAreaList.add(COMBATAREA_US);
    }

    static int delay = 0;

    @Override
    public void run() {
        check();
    }

    /**
     * 检查玩家是否在战斗区域之外
     */
    public static void check() {
        for (Player p : Bukkit.getOnlinePlayers()) {
            if(p.getHealth() == 0 || p.isOp())
                continue;
            boolean out = false;
            for (CombatArea combatArea : combatAreaList) {
                //if(!Objects.equals(GameManager.getInstance().currentGame.getMapName(), combatArea.getWorld())) continue;
                if (isOutOfCombatArea(p, combatArea)) {
                    if (!Cooldown.isCool("OutOfCombatArea", p)) {
                        Cooldown cooldown = new Cooldown("OutOfCombatArea", combatArea.getTime(), p);
                        cooldown.startForSinglePlayer();
                        p.playSound(p.getLocation(),"custom.OutOfArea",100.0F,1.0F);
                        p.playSound(p.getLocation(),"faction.OutOfArea_" + getCountry2(p),100.0F,1.0F);
                        VexImage icon = new VexImage("[local]Area/OutOfArea_" + (Cooldown.getTime("OutOfCombatArea", p) - 1) + ".png", -1, -1, 200, 200);
                        VexViewAPI.sendHUD(p, new VexImageShow("OutOfArea", icon, 1));
                    } else {
                        int timeLeft = Cooldown.getTime("OutOfCombatArea", p);
                        if(timeLeft > 1) {
                            VexImage icon = new VexImage("[local]Area/OutOfArea_" + (timeLeft - 1) + ".png", -1, -1, 200, 200);
                            VexViewAPI.sendHUD(p, new VexImageShow("OutOfArea", icon, 1));
                            if(timeLeft < 6) {
                                if(delay++ == 3) {
                                    p.playSound(p.getLocation(),"custom.OutOfBounds_Count",100.0F,1.0F);
                                    delay = 0;
                                }
                            }
                        }
                        else {
                            p.setHealth(0);
                            GameManager.getInstance().currentGame.subTicket(p,2);
                            p.playSound(p.getLocation(),"custom.OutOfBounds_End",100.0F,1.0F);
                            Bukkit.broadcastMessage("§e[管理员]"+p.getName()+"因脱离战斗区域而被枪毙!");
                        }
                    }
                    out = true;
                }
            }
            if(!out) {
                Cooldown.remove("OutOfCombatArea", p);
            }
        }
    }
}
